﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Testing
{
    public partial class Form2 : Form
    {
            private Line CurrentLine; 
            private bool AddingLine; 
            private LineArray Lines=new LineArray(); 
            private Point LastPos; 
            private bool first = true; 

            public Form2() 
            { 
                  InitializeComponent(); 
            } 

  
            private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 
            { 
                  CurrentLine = new Line(); 
                  CurrentLine.StartPoint=new Point(e.X,e.Y); 
                  AddingLine = true; 
            } 

            private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) 
            { 
                  Graphics g=CreateGraphics(); 
                  if(!AddingLine) 
                  { 
                        foreach(Line l in Lines) 
                        { 
                              if(l.IsInLine(new Point(e.X,e.Y))) 
                                    l.DrawLine(g,Color.Red); 
                              else 
                                    l.DrawLine(g,Color.Black); 
                        } 
                        LastPos = new Point(e.X,e.Y); 
                  } 
                  else 
                  { 
                        if(!first) 
                              ControlPaint.DrawReversibleLine(PointToScreen(CurrentLine.StartPoint), PointToScreen(LastPos), Color.White); 
                        LastPos = new Point(e.X,e.Y); 
                        ControlPaint.DrawReversibleLine(PointToScreen(CurrentLine.StartPoint), PointToScreen(LastPos), Color.White);
                        first=false; 
                  } 
            } 

            private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) 
            { 
                  CurrentLine.EndPoint=new Point(e.X,e.Y); 
                  CurrentLine.PenWidth=1; 
                  Lines.Add(CurrentLine); 
                  AddingLine = false; 
                  Invalidate(); 
            } 

            private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) 
            {
                foreach (Line l in Lines)
                    l.DrawLine(e.Graphics, Color.Black); 
            } 
      } 


      public class Line 
      { 
            public Point StartPoint; 
            public Point EndPoint; 
            public int PenWidth; 

            public void DrawLine(Graphics g,Color c) 
            { 
                  Pen p=new Pen(c,PenWidth); 
                  g.DrawLine(p,StartPoint,EndPoint); 
                  p.Dispose(); 
            }

            public bool IsInLine(Point pnt)
            {
                Pen p = new Pen(Color.Black, PenWidth);
                GraphicsPath pth = new GraphicsPath();
                pth.AddLine(StartPoint, EndPoint);
                pth.Widen(p);
                p.Dispose();
                if (pth.IsVisible(pnt))
                    return true;
                return false;
            } 

      }

      public class LineArray : CollectionBase
      {

          public void Add(Line l)
          {
              List.Add(l);
          }

          public Line this[int index]
          {
              get { return (Line)List[index]; }
              set { List[index] = value; }
          }
      }
}
